<?php

namespace App\Http\Controllers;

use App\Http\Biz\SuppManagerBiz;
use App\Http\Utils\Common\Consts;
use App\Http\Utils\Common\OperResult;
use App\Http\Utils\Common\WechatUtils;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Log;
use ErrorException;


class SuppManagerController extends Controller
{

    public function template(Request $request)
    {
        $complate_id = Consts::TEMPLATE_ID;
        $own_id = 18;
        $account_type = 1;
        $openids = ['obZDz0JlXXI_KmtiNneXp41dq54g'];//132账号门店的openid
        $params = [
            'keyword1'=> [
                "value"=>"舟山渔场",
                "color"=>"#173177"
            ],
            'keyword2'=> [
                "value"=>Carbon::now(),
                "color"=>"#173177"
            ],
            'keyword3'=> [
                "value"=>"货物已经出库，请及时签收",
                "color"=>"#173177"
            ]
        ];
        $page = 'pages/shop/shop_storage/shop_storage?id=16';
        $util = new WechatUtils();
        $util->sendTemplate($complate_id, $own_id, $account_type, $openids,$params,$page);
    }

    //删除账号关系
    public function deleteAccountRole(Request $request) {
        try {
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'start');
            if (!$request->has('supp_id') || !$request->has('id') || !$request->has('account_id'))
                return response()->json(['code'=>OperResult::CODE_CREATE_FAIL, 'msg'=>OperResult::CODE_CREATE_FAIL_DESC]);
            $tmpBiz = new SuppManagerBiz();
            $bool = $tmpBiz->is_superman($request->shop_id,$request->account_id);
            if(!$bool){
                return response()->json(['code' => OperResult::CODE_UPDATE_FAIL, 'msg' => OperResult::CODE_CREATE_FAIL_DESC]);
            }
            $data = $tmpBiz->deleteAccountRoleBiz($request->supp_id,$request->account_id,$request->id);
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'end');
            return response()->json(['code'=>OperResult::CODE_READ_OK, 'msg'=>OperResult::CODE_READ_OK_DESC]);
        } catch (ErrorException $ex) {
            Log::error(get_class($this).' exception '.print_r($ex->getMessage(), true));
            return response()->json(['code'=>OperResult::CODE_READ_FAIL, 'msg'=>OperResult::CODE_READ_FAIL_DESC]);
        }
    }

    //更新权限
    public function updateAccountRole(Request $request) {
        try {
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'start');
            if (!$request->has('supp_id') || !$request->has('account_id') || !$request->has('role_id'))
                return response()->json(['code'=>OperResult::CODE_CREATE_FAIL, 'msg'=>OperResult::CODE_CREATE_FAIL_DESC]);
            $tmpBiz = new SuppManagerBiz();
            $bool = $tmpBiz->is_superman($request->shop_id,$request->account_id);
            if(!$bool){
                return response()->json(['code' => OperResult::CODE_UPDATE_FAIL, 'msg' => OperResult::CODE_CREATE_FAIL_DESC]);
            }
            $data = $tmpBiz->updateAccountRoleBiz($request->supp_id,$request->account_id,$request->role_id);
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'end');
            return response()->json(['code'=>OperResult::CODE_READ_OK, 'msg'=>OperResult::CODE_READ_OK_DESC]);
        } catch (ErrorException $ex) {
            Log::error(get_class($this).' exception '.print_r($ex->getMessage(), true));
            return response()->json(['code'=>OperResult::CODE_READ_FAIL, 'msg'=>OperResult::CODE_READ_FAIL_DESC]);
        }
    }

    //查看账号详情
    public function getAccountDetailById(Request $request) {
        try {
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'start');
            if (!$request->has('supp_id') || !$request->has('account_id') || !$request->has('role_id'))
                return response()->json(['code'=>OperResult::CODE_CREATE_FAIL, 'msg'=>OperResult::CODE_CREATE_FAIL_DESC, 'data'=>null]);
            $tmpBiz = new SuppManagerBiz();
            $bool = $tmpBiz->is_superman($request->shop_id,$request->account_id);
            if(!$bool){
                return response()->json(['code' => OperResult::CODE_UPDATE_FAIL, 'msg' => OperResult::CODE_CREATE_FAIL_DESC]);
            }
            $data = $tmpBiz->getAccountDetailByIdBiz($request->supp_id,$request->account_id,$request->role_id);
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'end');
            return response()->json(['code'=>OperResult::CODE_READ_OK, 'msg'=>OperResult::CODE_READ_OK_DESC, 'data'=>$data]);
        } catch (ErrorException $ex) {
            Log::error(get_class($this).' exception '.print_r($ex->getMessage(), true));
            return response()->json(['code'=>OperResult::CODE_READ_FAIL, 'msg'=>OperResult::CODE_READ_FAIL_DESC, 'data'=>null]);
        }
    }
    
    //管理员账号列表
    public function managerList(Request $request)
    {
        try {
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'start');
            if (!$request->has('supp_id') || !$request->has('account_id'))
                return response()->json(['code'=>OperResult::CODE_CREATE_FAIL, 'msg'=>OperResult::CODE_CREATE_FAIL_DESC, 'data'=>array(),'roles'=>[]]);
            $tmpBiz = new SuppManagerBiz();
            $bool = $tmpBiz->is_superman($request->supp_id,$request->account_id);
            if(!$bool){
                return response()->json(['code' => OperResult::CODE_UPDATE_FAIL, 'msg' => OperResult::CODE_CREATE_FAIL_DESC]);
            }
            $data = $tmpBiz->managerListBiz($request->supp_id,$request->account_id);
            $roles = $tmpBiz->roleListBiz($request->supp_id);
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'end');
            return response()->json(['code'=>OperResult::CODE_READ_OK, 'msg'=>OperResult::CODE_READ_OK_DESC, 'data'=>$data,'roles'=>$roles]);
        } catch (ErrorException $ex) {
            Log::error(get_class($this).' exception '.print_r($ex->getMessage(), true));
            return response()->json(['code'=>OperResult::CODE_READ_FAIL, 'msg'=>OperResult::CODE_READ_FAIL_DESC, 'data'=>array(),'roles'=>[]]);
        }
    }

    //添加账号权限
    public function addmanager(Request $request) {
        try {
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'start');
            if (!$request->has('supp_id') || !$request->has('account_id') || !$request->has('account'))
                return response()->json(['code'=>OperResult::CODE_CREATE_FAIL, 'msg'=>OperResult::CODE_CREATE_FAIL_DESC]);
            $tmpBiz = new SuppManagerBiz();
            $bool = $tmpBiz->is_superman($request->shop_id,$request->account_id);
            if(!$bool){
                return response()->json(['code' => OperResult::CODE_UPDATE_FAIL, 'msg' => OperResult::CODE_CREATE_FAIL_DESC]);
            }
            $tmpBiz->addmanagerBiz($request->supp_id,$request->account_id,$request->account,$request->role_id);
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'end');
            return response()->json(['code'=>OperResult::CODE_READ_OK, 'msg'=>OperResult::CODE_READ_OK_DESC]);
        } catch (ErrorException $ex) {
            Log::error(get_class($this).' exception '.print_r($ex->getMessage(), true));
            return response()->json(['code'=>OperResult::CODE_READ_FAIL, 'msg'=>OperResult::CODE_READ_FAIL_DESC]);
        }
    }
}
